package geograph;

import geograph.china.City;
import geograph.china.Processor;
import geograph.china.Province;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Title：
 * Description：
 * Created by Myth on 8/18/2017.
 */
public class Geograph extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // web应用启动之后，直接使用proessor提取出来中国各省市信息存在context里面
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = response.getWriter();

        ServletContext servletContext = this.getServletContext();
        Processor processor = (Processor) servletContext.getAttribute("processor");
        if (processor == null) {
            String path = servletContext.getRealPath("") + "data.txt";
            //writer.write(path);
            processor = new Processor();
            try {
                processor.processFile(path);
            } catch (Exception e) {
                e.printStackTrace();
            }
            servletContext.setAttribute("processor", processor);
        }

        String province = request.getParameter("province");
        String city = request.getParameter("city");

        // 使用方法：在url: www.caipengbo.cn/geograph后依次加province、city参数(地点的英文名字)获取数据。
        String returnString;
        try {
            if (province == null && city == null) {  // 省份列表
                returnString = processor.getJsonArrayString(processor.getProvinceList());
            } else if (province != null && city == null) { // 城市列表
                Province p = processor.searchProvince(province);
                if (p != null) {
                    returnString = processor.getJsonArrayString(p.getCityList());
                } else {
                    returnString = "error";
                }
            } else if (province != null && city != null ) { // 地区列表
                Province p = processor.searchProvince(province);
                if (p != null) {
                    City c = p.searchCity(city);
                    if (c != null) {
                        returnString = processor.getJsonArrayString(c.getDistrictList());
                    } else {
                        returnString = "error";
                    }
                } else {
                    returnString = "error";
                }
            } else {
                returnString = "error";
            }
        } catch (Exception e) {
            returnString = "error";
        }

        writer.write(returnString);
    }
}
